﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Modules.Catalog.Core.Abstractions;
using Modules.Catalog.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modules.Catalog.Core.Queries
{
    public class GetAllBrandsQuery : IRequest<IEnumerable<Brand>>
    {
    }

    public class BrandQueryHandler : IRequestHandler<GetAllBrandsQuery, IEnumerable<Brand>>
    {
        private readonly ICatalogDbContext catalogDbContext;

        public BrandQueryHandler(ICatalogDbContext catalogDbContext)
        {
            this.catalogDbContext = catalogDbContext;
        }

        public async Task<IEnumerable<Brand>> Handle(GetAllBrandsQuery request, CancellationToken cancellationToken)
        {
            var brands=await catalogDbContext.Brands.OrderBy(o=>o.Id).ToListAsync(cancellationToken);
            if (brands.Any()) { 
                return brands;
            }
            throw new Exception("Brand Not Found!");
        }
    }
}
